<!--
  选人
-->
<template>
  <a-modal
    :open="visible"
    :width="960"
    :body-style="{ height: '520px', overflow: 'hidden' }"
    wrap-class-name="common-content user-select-content vxe-table--ignore-clear"
    :centered="true"
    :keyboard="false"
    :mask-closable="false"
    :destroy-on-close="true"
    @cancel="onClose"
  >
    <div class="popup-title">
      <h2>
        {{ title }}
        <a-tooltip v-if="titleTips" placement="top">
          <template #title>
            <span>{{ titleTips }}</span>
          </template>
          <avic-icon svg="avic-question-fill" />
        </a-tooltip>
      </h2>
    </div>
    <div class="popup-content">
      <UserSelectContent
        ref="userSelectContent"
        :name="name"
        :title="title"
        :title-tips="titleTips"
        :search-placeholder="searchPlaceholder"
        :select-model="selectModel"
        :max-select="maxSelect"
        :default-value="internaldefaultValue"
        :model-change="modelChange"
        :view-scope="viewScope"
        :default-org-id="defaultOrgId"
        :secret-level="secretLevel"
        :user-secret-level="userSecretLevel"
        :default-load-dept-id="defaultLoadDeptId"
        :default-load-group-id="defaultLoadGroupId"
        :default-load-role-id="defaultLoadRoleId"
        :default-load-position-id="defaultLoadPositionId"
        :view-system-user="viewSystemUser"
        :user-url="userUrl"
        :method-type="methodType"
        :user-search-url="userSearchUrl"
        :dept-tab-expand-level="deptTabExpandLevel"
        :search-num-limit="searchNumLimit"
        :custom-param="customParam || {}"
        :custom-search-param="customSearchParam || {}"
        :user-select-tabs="userSelectTabs"
        :get-init-user-info-url="getInitUserInfoUrl"
        :get-init-user-info-method="getInitUserInfoMethod"
        :more-user-info="moreUserInfo"
        :show-more-user-info="showMoreUserInfo"
        :filter-user="filterUser"
        :check-childen-node="checkChildenNode"
        :view-type="viewType"
        @defaultBack="defaultBack"
        @callBack="callBack"
        @close="close"
      />
    </div>
    <template #footer>
      <a-button title="确认" type="primary" @click="onConfirm">确认</a-button>
      <a-button title="取消" type="primary" ghost @click="onClose">取消</a-button>
    </template>
  </a-modal>
</template>
<!-- eslint-disable max-lines -->
<script lang="ts" setup>
import UserSelectContent from './UserSelectContent.vue';
const props = defineProps({
  /** 是否显示 */
  show: { type: Boolean, required: true, default: false },
  /** 右侧列表已选标题文案 */
  name: { type: String, default: '人员' },
  /** 标题 */
  title: { type: String, default: '人员选择' },
  /** 辅助提示信息 */
  titleTips: { type: String, default: '' },
  /** 搜索提示 */
  searchPlaceholder: { type: String, default: '请输入用户信息' },
  /** 单选 single ，多选 multi */
  selectModel: { type: String, default: 'single' },
  /** 多选最多选择数量 */
  maxSelect: { type: Number, default: 0 },
  /** 默认值 */
  defaultValue: { type: [Object, String], default: '' },
  /** 双向绑定值 */
  modelChange: {
    type: Object,
    default: () => ({ ids: '', names: '' })
  },
  /** tab页签集合 */
  userSelectTabs: { type: Array, default: () => [] },
  /** 展示人员的额外信息 ,可选列表 */
  moreUserInfo: { type: Array, default: () => [] },
  /** 是否展示人员附加信息 */
  showMoreUserInfo: { type: [Boolean, Array], default: true },
  /** 组织范围 */
  viewScope: { type: String, default: '' },
  /** 默认加载组织id */
  defaultOrgId: { type: String, default: '' },
  /** 默认加载部门id */
  defaultLoadDeptId: { type: String, default: '' },
  /** 默认加载群组id */
  defaultLoadGroupId: { type: String, default: '' },
  /** 默认加载角色id */
  defaultLoadRoleId: { type: String, default: '' },
  /** 默认加载岗位id */
  defaultLoadPositionId: { type: String, default: '' },
  /** 选人-文档密级过滤 */
  secretLevel: { type: String, default: '' },
  /** 选人-人员密级过滤 */
  userSecretLevel: { type: String, default: '' },
  /** 是否展示无效用户 */
  viewSystemUser: { type: Boolean, default: false },
  /** 部门tab页初始展开层级 */
  deptTabExpandLevel: { type: Number, default: 2 },
  /** 关键字查询时最大用户数限制 */
  searchNumLimit: { type: Number, default: 200 },
  /** 自定义参数 */
  customParam: { type: Object, default: null },
  /** 搜索自定义参数 */
  customSearchParam: { type: Object, default: null },
  /** 获取初始化用户信息请求地址 */
  getInitUserInfoUrl: { type: String, default: '/appsys/common/select/getSelectedUserInfo/v1' },
  /** 获取初始化用户信息请求类型 */
  getInitUserInfoMethod: { type: String, default: 'post' },
  /** 用户请求地址 */
  userUrl: { type: String, default: '/appsys/common/select/getUserSelectList/v1' },
  /** 用户请求类型 */
  methodType: { type: String, default: 'post' },
  /** 搜索用户请求地址 */
  userSearchUrl: { type: String, default: '/appsys/common/select/getUserSelectList/v1' },
  /** 根据业务需要过滤可选人员数据的接口(sync) */
  filterUser: { type: Function, default: null },
  /** check时选中孙子节点,打开时会层层异步加载子节点的子节点，关闭时只加载一层子节点 */
  checkChildenNode: { type: Boolean, default: true },
  /** 用户、角色、群组、岗位三个维度待选区的展示视图类型 table tree  */
  viewType: { type: String, default: 'tree' }
});
const userSelectContent = ref(null);
const visible = ref(false);
const internaldefaultValue = reactive({ ids: '', names: '' }); // 默认选中值

watch(
  () => props.show,
  (newV) => {
    visible.value = newV;
    // if (newV === true) {
    //   init();
    // }
  },
  { immediate: true }
);
watch(
  () => props.defaultValue,
  (newValue: any, oldValue: any) => {
    if (!newValue) {
      internaldefaultValue.ids = '';
      internaldefaultValue.names = '';
    } else if (JSON.stringify(newValue) !== JSON.stringify(oldValue)) {
      updateInternalDefaultValue(newValue);
    }
  },
  { immediate: true }
);

const $emit = defineEmits(['defaultBack', 'callBack', 'close']);
function defaultBack(result) {
  $emit('defaultBack', result);
}
function callBack(value, selectRows) {
  $emit('callBack', value, selectRows);
}
function close() {
  visible.value = false;
}
/**
 * 点击确定事件
 */
function onConfirm() {
  const result = userSelectContent.value.setResultData();
  const { selectList } = userSelectContent.value;
  if (result.ids) {
    $emit('callBack', result, selectList);
  } else {
    $emit('callBack', {});
  }
  userSelectContent.value.emptyQuery();
  // 选择值之后更新
  visible.value = false;
  // 选中之后需要更新默认选择项为选择后的值
  setInternaldefaultValue(result, selectList);
}
// 选中之后需要更新默认选择项为选择后的值
function setInternaldefaultValue(result, selectList) {
  internaldefaultValue.ids = result.ids;
  internaldefaultValue.names = result.names;
  if (typeof props.showMoreUserInfo == 'object') {
    props.showMoreUserInfo.forEach((key: string) => {
      internaldefaultValue[key] = selectList.map((item) => item.attributes[key]).join(';');
    });
  } else if (props.showMoreUserInfo) {
    props.moreUserInfo.forEach((key: string) => {
      internaldefaultValue[key] = selectList.map((item) => item.attributes[key]).join(';');
    });
  }
}
// 监听父组件传入的值更新默认值
function updateInternalDefaultValue(newValue) {
  internaldefaultValue.ids = newValue.ids;
  internaldefaultValue.names = newValue.names;
  if (typeof props.showMoreUserInfo == 'object') {
    props.showMoreUserInfo.forEach((key: string) => {
      newValue[key] ? (internaldefaultValue[key] = newValue[key]) : '';
    });
  } else if (props.showMoreUserInfo) {
    props.moreUserInfo.forEach((key: string) => {
      newValue[key] ? (internaldefaultValue[key] = newValue[key]) : '';
    });
  }
}
/**
 * 点击取消、关闭事件
 */
function onClose() {
  userSelectContent.value.emptyQuery();
  $emit('close');
}
</script>

<style lang="less" scoped>
:deep(.container-left) {
  width: 100%;
  .ant-tabs-content-holder {
    min-height: 420px;

    .ant-tree {
      height: auto;
    }
  }
}
</style>
